package day09;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class TestInputStreamReader {

    public static void main(String[] args) {
        try {
            writer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 输入流
    public static void read() throws IOException {
        InputStream is = new FileInputStream("src/test11/hello.txt");
        Reader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
        char[] c = new char[10];
        int len;
        while ((len = isr.read(c)) != -1) {
            String str = new String(c, 0, len);
            System.out.println(str);
        }
    }

    // 读写操作,同时修改编码
    public static void writer() throws IOException {

        InputStream is = new FileInputStream("src/test11/hello.txt");
        Reader isr = new InputStreamReader(is, StandardCharsets.UTF_8);

        OutputStream os = new FileOutputStream("src/test11/hello_out_put.txt");
        Writer osw = new OutputStreamWriter(os, "GBK");

        char[] c = new char[10];
        int len;
        while ((len = isr.read(c)) != -1) {
            osw.write(c, 0, len);
        }

        isr.close();
        osw.close();
    }

}
